/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.xs_novel_chapter.service;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jeeplus.modules.xs_book.entity.XsBook;
import com.jeeplus.modules.xs_book.mapper.XsBookMapper;
import com.jeeplus.modules.xs_notice.entity.XsNotice;
import com.jeeplus.modules.xs_writer.entity.XsWriter;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.xs_novel_chapter.entity.XsNovelChapter;
import com.jeeplus.modules.xs_novel_chapter.mapper.XsNovelChapterMapper;

/**
 * 小说章节信息Service
 * @author 天竹子
 * @version 2021-07-20
 */
@Service
@Transactional(readOnly = true)
public class XsNovelChapterService extends CrudService<XsNovelChapterMapper, XsNovelChapter> {

	@Autowired
	private XsBookMapper xsBookMapper;

	public XsNovelChapter get(String id) {
		return super.get(id);
	}
	
	public List<XsNovelChapter> findList(XsNovelChapter xsNovelChapter) {
		return super.findList(xsNovelChapter);
	}
	
	public Page<XsNovelChapter> findPage(Page<XsNovelChapter> page, XsNovelChapter xsNovelChapter) {
		return super.findPage(page, xsNovelChapter);
	}
	
	@Transactional(readOnly = false)
	public void save(XsNovelChapter xsNovelChapter) {
		XsWriter xsWriter = null;
		/*if(Strings.isNotBlank(xsNovelChapter.getId())) {
			XsNovelChapter xsNovelChapter1 = super.get(xsNovelChapter);
			if ("2".equals(xsNovelChapter1.getIsPass()) && "1".equals(xsNovelChapter.getIsPass())) {
				XsBook xsBook = xsBookMapper.get(xsNovelChapter.getBook());
				xsBook.setWordNumber(xsBook.getWordNumber() + xsNovelChapter.getWordNumber());
				xsBook.setUpdateDate(new Date());
				xsBookMapper.update(xsBook);
			}
		}*/
		XsBook xsBook = xsBookMapper.get(xsNovelChapter.getBook());
		xsNovelChapter.setWriter(xsBook.getWriter());
		Pattern p_html = Pattern.compile("<[a-zA-z]{1,9}((?!>).)*>", Pattern.CASE_INSENSITIVE);
		Pattern t_html = Pattern.compile("</[a-zA-z]{1,9}>", Pattern.CASE_INSENSITIVE);
		String s = xsNovelChapter.getContent();
		Matcher m_script = p_html.matcher(s);
		s = m_script.replaceAll("");
		Matcher l_script = t_html.matcher(s);
		int length = l_script.replaceAll("").length();
		xsNovelChapter.setWordNumber(length);
		XsNovelChapter xsNovelChapter1 = new XsNovelChapter();
		xsNovelChapter1.setBook(xsNovelChapter.getBook());
		List<XsNovelChapter> list = super.findList(xsNovelChapter1);
		xsBook.setWordNumber(list.stream().mapToInt(XsNovelChapter::getWordNumber).sum());
		xsBookMapper.update(xsBook);
		if (xsNovelChapter.getWordNumber() > 1000) {
			xsNovelChapter.setCharge(5 + (xsNovelChapter.getWordNumber() - 1000) % 100 == 0 ? (xsNovelChapter.getWordNumber() - 1000) / 100 : (xsNovelChapter.getWordNumber() - 1000) / 100 + 1 * 0.5);
		} else {
			xsNovelChapter.setCharge(5.0);
		}
		super.save(xsNovelChapter);
	}
	
	@Transactional(readOnly = false)
	public void delete(XsNovelChapter xsNovelChapter) {
		super.delete(xsNovelChapter);
	}
	
}